import org.apache.commons.pool2.impl.GenericObjectPool;

public class TestMain {

    public static void main(String[] args) throws Exception {
       
        /* 验证在不归还的情况下池子是否可以无限借*/
        //new TestMain().test1();
        
        /* 验证在归还的情况下池子是否可以无限借*/
        //new TestMain().test2();
        
        /* 验证模拟线程等待时借用的情况*/
        //new TestMain().test3();

    }
    
    private void test1() throws Exception {
        GenericObjectPool<OrderService> pool = new GenericObjectPool<OrderService>(new OrderServicePool());

        /*
         *pool配置 
         */
        pool.setMaxTotal(3);
        
        for (int i = 0; i < 20; i++) {
            System.out.println("------------------------------" + pool.getBorrowedCount());
            OrderService orderService1 = pool.borrowObject();
            orderService1.login();
            System.out.println("first" + i);
        }
        
        long borrowedCount = pool.getBorrowedCount();
        System.out.println(borrowedCount);
    }
    
    
    private void test2() throws Exception {
        GenericObjectPool<OrderService> pool = new GenericObjectPool<OrderService>(new OrderServicePool());
        
        /*
         *pool配置 
         */
        pool.setMaxTotal(3);
        
        for (int i = 0; i < 20; i++) {
            System.out.println("------------------------------" + pool.getBorrowedCount());
            OrderService orderService1 = pool.borrowObject();
            orderService1.login();
            System.out.println("first" + i);
            pool.returnObject(orderService1);
        }
        
        long borrowedCount = pool.getBorrowedCount();
        System.err.println("last: " + borrowedCount);
        
    }
    
    /**
     * 模拟线程等待测试
     * TODO Comment.
     * 
     * @throws Exception
     * @since v#{version}
     * @author zhangxf
     * @created 2015年12月23日 上午9:05:58
     */
    private void test3() throws Exception {
        final GenericObjectPool<OrderService> pool = new GenericObjectPool<OrderService>(new OrderServicePool());

        /*
         *pool配置 
         */
        pool.setMaxTotal(3);
        
        for (int i = 0; i < 9; i++) {
            new Thread(new Runnable() {
                
                @Override
                public void run() {
                    long c1 = System.currentTimeMillis();
                    OrderService orderService1;
                    try {
                        orderService1 = pool.borrowObject();
                        long c2 = System.currentTimeMillis();
                        System.out.println("借取对象耗时: " + (c2 - c1));
                        orderService1.login();
                        
                        Thread.sleep(100);
                        
                        pool.returnObject(orderService1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
        
        long borrowedCount = pool.getBorrowedCount();
        System.err.println("last: " + borrowedCount);
        
    }
}
